package com.cskaoyan.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-15
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
// prefix属性值+成员变量名(set) = 配置文件中的key
public class DataSourceConfiguration {

    // 配置类使用properties类
    //@Autowired
    DataSourceProperties properties;

    // 通过有参构造方法来实例化配置类，有参构造方法的形参来源于容器
    public DataSourceConfiguration(DataSourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriver());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }
}
